package com.zz.entity;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Float;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;

import com.zz.art.Art;
import com.zz.level.Level;
import com.zz.pathfinding.AStarPathFinder;
import com.zz.pathfinding.Path;
import com.zz.pathfinding.PathFinder;


public class Player{

	public String name;
	private Animation leftAnim;
	private Animation rightAnim;
	public Animation downAnim;
	private Animation upAnim;
	public Animation anim;
	public PathFinder finder;
	public boolean finished;
	private int speed;
	public Point2D.Float position;
	public Path path = new Path();
	public Level level;
	
	private static int count = -5;
	
	public Player(Level level, int color) {
		this.name = "Player " + count;
		//count ++;
		finder = new AStarPathFinder(level, 500, true);
		leftAnim = Art.players.get(color);
		rightAnim = Art.players.get(color + 6);
		downAnim = Art.players.get(color + 12);
		upAnim = Art.players.get(color + 18);
		anim = downAnim;
		
		finished = true;
		speed = 1;
		level.units[(int) position.x][(int) position.y] = this;
	}
	
	public Player(int color) {
		this.name = "Player " + count;
		count ++;
		leftAnim = Art.players.get(color);
		rightAnim = Art.players.get(color + 6);
		downAnim = Art.players.get(color + 12);
		upAnim = Art.players.get(color + 18);
		anim = downAnim;
		finder = new AStarPathFinder(500, true);
	}

	public Player(Player p) {
		name = p.name;
		leftAnim = p.leftAnim;
		rightAnim = p.rightAnim;
		downAnim = p.downAnim;
		upAnim = p.upAnim;
		anim = p.anim;
		finder = p.finder;
		finished = p.finished;
		speed = p.speed;
		position = p.position;
		path = p.path;
	}

	public void move(Level level) {
		if(!finished && !path.steps.isEmpty()) {
			int oldx = (int) this.position.x;
			int oldy = (int) this.position.y;
			this.position.setLocation(path.steps.get(0).getX(), path.steps.get(0).getY());
			anim = getOrientation(oldx, oldy);
			path.steps.remove(0);
			level.units[oldx][oldy] = null;
			if(path.steps.isEmpty())
				finished = true;
			else
				finished = false;
					
			level.units[(int) position.x][(int) position.y] = null;
			//if((up && right) || (up && left) || (down && right) || (down && left))
				//speed /= 1.4f;
			
			//speed = 0.3f;
			
		}
		level.units[(int) position.x][(int) position.y] = this;
		/*if(ss-- < 0)
			stop = true;*/
	}

	private Animation getOrientation(int oldx, int oldy) {
		
		int newx = (int) position.x, newy = (int) position.y;
		
		if(oldx == newx) {
			if(oldy == newy) {
				return anim;
			}
			if(oldy < newy)
				return downAnim;
			else
				return upAnim;
		}
		if(oldy == newy) {
			if(oldx < newx)
				return rightAnim;
			else
				return leftAnim;
		}
			
		return anim;
	}

	public void setPosition(Level level, Point2D.Float pos) {
		position = pos;
		level.units[(int) position.x][(int) position.y] = this;
		this.level = level;
		
	}
}

